package com.example.demo.http;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileInputStream;

/**
 * 
 * @Description: http工具类
 * @author: jw.chen
 * @Date: 2020-01-08
 *
 */
public class HttpUtils {


	/**
	 * HTTP接口调用(Post)
	 * @param url
	 * @param body
	 * @return
	 * @throws Exception
	 */
	public static String sendPostFormData(String url, String inFileUrl ,String outFileUrl) throws Exception {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		String result = null;
		try {
			httpClient = HttpClients.createDefault();
			HttpPost uploadFile = new HttpPost(url);
			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
			builder.addTextBody("path", outFileUrl, ContentType.TEXT_PLAIN);
			builder.addTextBody("output", "json", ContentType.TEXT_PLAIN);
			builder.addTextBody("scene", "image", ContentType.TEXT_PLAIN);
			// 把文件加到HTTP的post请求中
			File f = new File(inFileUrl);
			builder.addBinaryBody(
					"file",
					new FileInputStream(f),
					ContentType.APPLICATION_OCTET_STREAM,
					f.getName()
			);

			HttpEntity multipart = builder.build();
			uploadFile.setEntity(multipart);
			response = httpClient.execute(uploadFile);
			HttpEntity responseEntity = response.getEntity();
			result=EntityUtils.toString(responseEntity, "UTF-8");
			System.out.println("Post 返回结果"+result);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if( httpClient!=null ) {
				httpClient.close();
				response.close();
			}
		}
		return null;
	}


	/**
	 * HTTP接口调用(Post)
	 * @param url
	 * @param body
	 * @return
	 * @throws Exception
	 */
	public static String sendPost(String url, String body) throws Exception {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		String result = null;
		try {
			httpClient = HttpClientBuilder.create().build(); 
			HttpPost httpPost = new HttpPost(url);
			httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
			httpPost.setEntity(new ByteArrayEntity(body.getBytes("utf-8")));
			response = httpClient.execute(httpPost);
			result = EntityUtils.toString(response.getEntity());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if( httpClient!=null ) {
				httpClient.close();
				response.close();
			}
		}
		return null;
	}
	
	/**
	 * HTTP接口调用(Get)
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String sendGet(String url) throws Exception {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		try {
			httpClient = HttpClients.createDefault();
			HttpGet hget = new HttpGet(url);
			response = httpClient.execute(hget);
			HttpEntity entity = response.getEntity();
			return EntityUtils.toString(entity, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if( httpClient!=null ) {
				httpClient.close();
				response.close();
			}
		}
		return null;
	}
	
}
